home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / nslookup / RCS / subr.c,v < prev   
Encoding:
Text File  |  1988-11-27  |  11.0 KB  |  489 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     88.11.23.13.39.27;  author douglis;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @original src from monet.
  17. @
  18.  
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/*
  27.  * Copyright (c) 1985 Regents of the University of California.
  28.  * All rights reserved.
  29.  *
  30.  * Redistribution and use in source and binary forms are permitted
  31.  * provided that the above copyright notice and this paragraph are
  32.  * duplicated in all such forms and that any documentation,
  33.  * advertising materials, and other materials related to such
  34.  * distribution and use acknowledge that the software was developed
  35.  * by the University of California, Berkeley.  The name of the
  36.  * University may not be used to endorse or promote products derived
  37.  * from this software without specific prior written permission.
  38.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  39.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  40.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  41.  */
  42.  
  43. #ifndef lint
  44. static char sccsid[] = "@@(#)subr.c    5.12 (Berkeley) 7/23/88";
  45. #endif /* not lint */
  46.  
  47. /*
  48.  *******************************************************************************
  49.  *
  50.  *  subr.c --
  51.  *
  52.  *    Miscellaneous subroutines for the name server 
  53.  *    lookup program.
  54.  *  
  55.  *    Copyright (c) 1985 
  56.  *      Andrew Cherenson
  57.  *    U.C. Berkeley
  58.  *      CS298-26  Fall 1985
  59.  *
  60.  *******************************************************************************
  61.  */
  62.  
  63. #include <stdio.h>
  64. #include <strings.h>
  65. #include <sys/types.h>
  66. #include <netdb.h>
  67. #include <sys/socket.h>
  68. #include <netinet/in.h>
  69. #include <arpa/nameser.h>
  70. #include <signal.h>
  71. #include <setjmp.h>
  72. #include "res.h"
  73.  
  74.  
  75.  
  76. /*
  77.  *******************************************************************************
  78.  *
  79.  *  IntrHandler --
  80.  *
  81.  *    This routine is called whenever a control-C is typed. 
  82.  *    It performs three main functions:
  83.  *     - closes an open socket connection,
  84.  *     - closes an open output file (used by LookupHost, et al.),
  85.  *     - jumps back to the main read-eval loop.
  86.  *        
  87.  *    If a user types a ^C in the middle of a routine that uses a socket, 
  88.  *    the routine would not be able to close the socket. To prevent an 
  89.  *    overflow of the process's open file table, the socket and output 
  90.  *    file descriptors are closed by the interrupt handler.
  91.  *
  92.  *  Side effects:
  93.  *    If sockFD is valid, it is closed.
  94.  *    If filePtr is valid, it is closed.
  95.  *    Flow of control returns to the main() routine.
  96.  *
  97.  *******************************************************************************
  98.  */
  99.  
  100. int
  101. IntrHandler()
  102. {
  103.     extern jmp_buf env;
  104.  
  105.     if (sockFD >= 0) {
  106.     close(sockFD);
  107.     sockFD = -1;
  108.     }
  109.     if (filePtr != NULL && filePtr != stdout) {
  110.     fclose(filePtr);
  111.     filePtr = NULL;
  112.     }
  113.     printf("\n");
  114.     longjmp(env, 1);
  115. }
  116.  
  117.  
  118. /*
  119.  *******************************************************************************
  120.  *
  121.  *  Malloc --
  122.  *  Calloc --
  123.  *
  124.  *      Calls the malloc library routine with SIGINT blocked to prevent
  125.  *    corruption of malloc's data structures. We need to do this because 
  126.  *    a control-C doesn't kill the program -- it causes a return to the 
  127.  *    main command loop.
  128.  *
  129.  *    NOTE: This method doesn't prevent the pointer returned by malloc 
  130.  *    from getting lost, so it is possible to get "core leaks".
  131.  *
  132.  *    If malloc fails, the program exits.
  133.  *
  134.  *  Results:
  135.  *    (address)    - address of new buffer.
  136.  *
  137.  *******************************************************************************
  138.  */
  139.  
  140. char *
  141. Malloc(size)
  142.     int size;
  143. {
  144.     char     *ptr;
  145.     int     saveMask;
  146.     extern char *malloc();
  147.  
  148. #ifdef SYSV
  149.     sighold(SIGINT);
  150.     ptr = malloc((unsigned) size);
  151.     sigrelse(SIGINT);
  152. #else
  153.     saveMask = sigblock(sigmask(SIGINT));
  154.     ptr = malloc((unsigned) size);
  155.     (void) sigsetmask(saveMask);
  156. #endif SYSV
  157.     if (ptr == NULL) {
  158.     fflush(stdout);
  159.     fprintf(stderr, "malloc failed\n");
  160.     fflush(stderr);
  161.     abort();
  162.     /*NOTREACHED*/
  163.     } else {
  164.     return(ptr);
  165.     }
  166. }
  167.  
  168. char *
  169. Calloc(num, size)
  170.     register int num, size;
  171. {
  172.     char *ptr = Malloc(num*size);
  173.     bzero(ptr, num*size);
  174.     return(ptr);
  175. }
  176.  
  177.  
  178. /*
  179.  *******************************************************************************
  180.  *
  181.  *  PrintHostInfo --
  182.  *
  183.  *    Prints out the HostInfo structure for a host.
  184.  *
  185.  *******************************************************************************
  186.  */
  187.  
  188. void
  189. PrintHostInfo(file, title, hp)
  190.     FILE     *file;
  191.     char     *title;
  192.     register HostInfo *hp;
  193. {
  194.     register char         **cp;
  195.     register ServerInfo     **sp;
  196.     char             comma;
  197.     int              i;
  198.  
  199.     fprintf(file, "%-7s  %s", title, hp->name);
  200.  
  201.     if (hp->addrList != NULL) {
  202.         if (hp->addrList[1] != NULL) {
  203.         fprintf(file, "\nAddresses:");
  204.         } else {
  205.         fprintf(file, "\nAddress:");
  206.         }
  207.         comma = ' ';
  208.         i = 0;
  209.         for (cp = hp->addrList; cp && *cp; cp++) {
  210.         i++;
  211.         if (i > 4) {
  212.             fprintf(file, "\n\t");
  213.             comma = ' ';
  214.             i = 0;
  215.         }
  216.         fprintf(file,"%c %s", comma, inet_ntoa(*(struct in_addr *)*cp));
  217.         comma = ',';
  218.         }
  219.     }
  220.  
  221.     if (hp->aliases != NULL) {
  222.         fprintf(file, "\nAliases:");
  223.         comma = ' ';
  224.         i = 10;
  225.         for (cp = hp->aliases; cp && *cp && **cp; cp++) {
  226.         i += strlen(*cp) + 2;
  227.         if (i > 75) {
  228.             fprintf(file, "\n\t");
  229.             comma = ' ';
  230.             i = 10;
  231.         }
  232.         fprintf(file, "%c %s", comma, *cp);
  233.         comma = ',';
  234.         }
  235.     }
  236.  
  237.     if (hp->servers != NULL) {
  238.         fprintf(file, "\nServed by:\n");
  239.         for (sp = hp->servers; *sp != NULL ; sp++) {
  240.  
  241.         fprintf(file, "- %s\n\t",  (*sp)->name);
  242.  
  243.         comma = ' ';
  244.         i = 0;
  245.         for (cp = (*sp)->addrList; cp && *cp && **cp; cp++) {
  246.             i++;
  247.             if (i > 4) {
  248.             fprintf(file, "\n\t");
  249.             comma = ' ';
  250.             i = 0;
  251.             }
  252.             fprintf(file, 
  253.             "%c %s", comma, inet_ntoa(*(struct in_addr *)*cp));
  254.             comma = ',';
  255.         }
  256.         fprintf(file, "\n\t");
  257.  
  258.         comma = ' ';
  259.         i = 10;
  260.         for (cp = (*sp)->domains; cp && *cp && **cp; cp++) {
  261.             i += strlen(*cp) + 2;
  262.             if (i > 75) {
  263.             fprintf(file, "\n\t");
  264.             comma = ' ';
  265.             i = 10;
  266.             }
  267.             fprintf(file, "%c %s", comma, *cp);
  268.             comma = ',';
  269.         }
  270.         fprintf(file, "\n");
  271.         }
  272.     }
  273.  
  274.     fprintf(file, "\n\n");
  275. }
  276.  
  277. /*
  278.  *******************************************************************************
  279.  *
  280.  *  OpenFile --
  281.  *
  282.  *    Parses a command string for a file name and opens
  283.  *    the file.
  284.  *
  285.  *  Results:
  286.  *    file pointer    - the open was successful.
  287.  *    NULL        - there was an error opening the file or
  288.  *              the input string was invalid.
  289.  *
  290.  *******************************************************************************
  291.  */
  292.  
  293. FILE *
  294. OpenFile(string, file)
  295.     char *string;
  296.     char *file;
  297. {
  298.     char     *redirect;
  299.     FILE     *tmpPtr;
  300.  
  301.     /*
  302.      *  Open an output file if we see '>' or >>'.
  303.      *  Check for overwrite (">") or concatenation (">>").
  304.      */
  305.  
  306.     redirect = index(string, '>');
  307.     if (redirect == NULL) {
  308.         return(NULL);
  309.     }
  310.     if (redirect[1] == '>') {
  311.         sscanf(redirect, ">> %s", file);
  312.         tmpPtr = fopen(file, "a+");
  313.     } else {
  314.         sscanf(redirect, "> %s", file);
  315.         tmpPtr = fopen(file, "w");
  316.     }
  317.  
  318.     if (tmpPtr != NULL) {
  319.         redirect[0] = '\0';
  320.     }
  321.  
  322.     return(tmpPtr);
  323. }
  324.  
  325. /*
  326.  *******************************************************************************
  327.  *
  328.  *  DecodeError --
  329.  *
  330.  *    Converts an error code into a character string.
  331.  *
  332.  *******************************************************************************
  333.  */
  334.  
  335. char *
  336. DecodeError(result)
  337.     int result;
  338. {
  339.     switch(result) {
  340.         case NOERROR:     return("Success"); break;
  341.         case FORMERR:    return("Format error"); break;
  342.         case SERVFAIL:    return("Server failed"); break;
  343.         case NXDOMAIN:    return("Non-existent domain"); break;
  344.         case NOTIMP:    return("Not implemented"); break;
  345.         case REFUSED:    return("Query refused"); break;
  346.         case NOCHANGE:    return("No change"); break;
  347.         case NO_INFO:     return("No information"); break;
  348.         case ERROR:     return("Unspecified error"); break;
  349.         case TIME_OUT:     return("Timed out"); break;
  350.         case NONAUTH:     return("Non-authoritative answer"); break;
  351.         default:         break;
  352.     }
  353.     return("BAD ERROR VALUE"); 
  354. }
  355.  
  356. int
  357. StringToClass(class, dflt)
  358.     char *class;
  359.     int dflt;
  360. {
  361.     if (strcasecmp(class, "IN") == 0)
  362.         return(C_IN);
  363.     if (strcasecmp(class, "CHAOS") == 0)
  364.         return(C_CHAOS);
  365.     if (strcasecmp(class, "ANY") == 0)
  366.         return(C_ANY);
  367.     fprintf(stderr, "unknown query class: %s\n", class);
  368.     return(dflt);
  369. }
  370. /*
  371.  *******************************************************************************
  372.  *
  373.  *  StringToType --
  374.  *
  375.  *    Converts a string form of a query type name to its 
  376.  *    corresponding integer value.
  377.  *
  378.  *******************************************************************************
  379.  */
  380.  
  381. int
  382. StringToType(type, dflt)
  383.     char *type;
  384.     int dflt;
  385. {
  386.     if (strcasecmp(type, "A") == 0)
  387.         return(T_A);
  388.     if (strcasecmp(type, "NS") == 0)
  389.         return(T_NS);            /* authoritative server */
  390.     if (strcasecmp(type, "MX") == 0)
  391.         return(T_MX);            /* mail exchanger */
  392.     if (strcasecmp(type, "CNAME") == 0)
  393.         return(T_CNAME);        /* canonical name */
  394.     if (strcasecmp(type, "SOA") == 0)
  395.         return(T_SOA);            /* start of authority zone */
  396.     if (strcasecmp(type, "MB") == 0)
  397.         return(T_MB);            /* mailbox domain name */
  398.     if (strcasecmp(type, "MG") == 0)
  399.         return(T_MG);            /* mail group member */
  400.     if (strcasecmp(type, "MR") == 0)
  401.         return(T_MR);            /* mail rename name */
  402.     if (strcasecmp(type, "WKS") == 0)
  403.         return(T_WKS);            /* well known service */
  404.     if (strcasecmp(type, "PTR") == 0)
  405.         return(T_PTR);            /* domain name pointer */
  406.     if (strcasecmp(type, "HINFO") == 0)
  407.         return(T_HINFO);        /* host information */
  408.     if (strcasecmp(type, "MINFO") == 0)
  409.         return(T_MINFO);        /* mailbox information */
  410.     if (strcasecmp(type, "AXFR") == 0)
  411.         return(T_AXFR);            /* zone transfer */
  412.     if (strcasecmp(type, "MAILB") == 0)
  413.         return(T_MAILB);        /* mail box */
  414.     if (strcasecmp(type, "ANY") == 0)
  415.         return(T_ANY);            /* matches any type */
  416.     if (strcasecmp(type, "UINFO") == 0)
  417.         return(T_UINFO);        /* user info */
  418.     if (strcasecmp(type, "UID") == 0)
  419.         return(T_UID);            /* user id */
  420.     if (strcasecmp(type, "GID") == 0)
  421.         return(T_GID);            /* group id */
  422.     fprintf(stderr, "unknown query type: %s\n", type);
  423.     return(dflt);
  424. }
  425.  
  426. /*
  427.  *******************************************************************************
  428.  *
  429.  *  DecodeType --
  430.  *
  431.  *    Converts a query type to a descriptive name.
  432.  *    (A more verbose form of p_type.)
  433.  *
  434.  *
  435.  *******************************************************************************
  436.  */
  437.  
  438. static  char nbuf[20];
  439.  
  440. char *
  441. DecodeType(type)
  442.     int type;
  443. {
  444.     switch (type) {
  445.     case T_A:
  446.         return("address");
  447.     case T_NS:
  448.         return("name server");
  449.     case T_MX:        
  450.         return("mail exchanger");
  451.     case T_CNAME:        
  452.         return("cannonical name");
  453.     case T_SOA:        
  454.         return("start of authority zone");
  455.     case T_MB:        
  456.         return("mailbox domain name");
  457.     case T_MG:        
  458.         return("mail group member");
  459.     case T_MR:        
  460.         return("mail rename name");
  461.     case T_NULL:        
  462.         return("null resource record");
  463.     case T_WKS:        
  464.         return("well known service");
  465.     case T_PTR:        
  466.         return("domain name pointer");
  467.     case T_HINFO:        
  468.         return("host");
  469.     case T_MINFO:        
  470.         return("mailbox (MINFO)");
  471.     case T_AXFR:        
  472.         return("zone transfer");
  473.     case T_MAILB:        
  474.         return("mail box");
  475.     case T_ANY:        
  476.         return("any type");
  477.     case T_UINFO:
  478.         return("user info");
  479.     case T_UID:
  480.         return("user id");
  481.     case T_GID:
  482.         return("group id");
  483.     default:
  484.         (void) sprintf(nbuf, "%d", type);
  485.         return (nbuf);
  486.     }
  487. }
  488. @
  489.